package com.l2qq.vben.auth;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

import java.util.List;
import java.util.stream.Collectors;

public class PermissionService {

    public List<String> getUserPermissions(int userId) {
        String sql = "SELECT p.key FROM permission p " + "JOIN role_permission rp ON p.id = rp.permission_id " + "JOIN user_role ur ON ur.role_id = rp.role_id " + "WHERE ur.user_id = ?";
        List<Record> records = Db.find(sql, userId);
        return records.stream().map(r -> r.getStr("key")).collect(Collectors.toList());
    }

    public List<String> getUserRoles(int userId) {
        String sql = "SELECT r.key FROM role r " + "JOIN user_role ur ON r.id = ur.role_id " + "WHERE ur.user_id = ?";
        List<Record> records = Db.find(sql, userId);
        return records.stream().map(r -> r.getStr("key")).collect(Collectors.toList());
    }
}